<?php
namespace Tyrone\Model;
use Medoo\Medoo;

class PlayerOnline extends Base
{
    protected $table = 'c_player_online';



    public function getDayPlayerData($day,$where=[])
    {
        $list = $this->database->select($this->table, [
            'contin_day',
            'contin_pay',
            'player_id',
        ], $where + [
            'day' => $day,
        ]);
        return $list ? array_column($list, null, 'player_id') : [];
    }

    public function updateData($items)
    {
        $item = $items[0] ?? [];
        if(!$item) return [];

        $uniqueKey = ['day','platform','plat_server','server_id','player_id'];
        $uniqueData = array_intersect_key($item,array_flip($uniqueKey));
        $update = array_diff_key($item,array_flip($uniqueKey));
        $this->database->batchUpdateOrInsert($this->table,$items,$uniqueKey);

        return $item;
    }

    public function playerLoginKeep($loginDay, $regDay, $where=[], $alias=[])
    {

        return $this->database->select($this->table.'(a)',[
            '[><]'.$this->table.'(b)' =>['a.player_id' => 'player_id']
        ],$alias + [
                'player_num' => Medoo::raw('COUNT(b.player_id)'),
                'b.platform',
                'b.plat_server',
            ],$where + [
                'a.day' => $loginDay,
                'b.day' => $regDay,
                'b.is_new' => 1,
                'GROUP' => ['b.platform','b.plat_server']
            ]);
    }

    public function playerRegister($day, $where =[], $alias=[])
    {

        return $this->database->select($this->table,$alias + [
            'player_num' => Medoo::raw('COUNT(player_id)'),
            'platform',
            'plat_server',
        ],$where + [
            'day' => $day,
            'is_new' => 1,
            'GROUP' => ['platform','plat_server']
        ]);
    }
    public function playerPay($day, $where =[], $alias=[])
    {

        return $this->database->select($this->table,$alias + [
                'player_num' => Medoo::raw('COUNT(player_id)'),
                'platform',
                'plat_server',
            ],$where + [
                'day' => $day,
                'is_pay' => 1,
                'GROUP' => ['platform','plat_server']
            ]);
    }

    public function playerPayKeep($loginDay, $regDay, $where=[], $alias=[]): array
    {

        return $this->database->select($this->table,$alias + [
            'player_num' => Medoo::raw('COUNT(player_id)'),
            'plat_server',
            'platform'
        ],$where + [
            'day' => $loginDay,
            'player_id' => Medoo::raw('(select player_id from c_player_online where day = '.$regDay.' and is_new = 1 and is_pay = 1)'),
            'GROUP' => ['platform','plat_server']
        ]);
    }


    public function getLoginPlayerPay($day,$where=[]){

        $list = $this->database->select($this->table.'(p)',[
            '[><]'.OLD_CENTER_DB.'.l_player_pay_total(pt)' =>[
                'p.player_id' => 'player_id',
                'p.platform' =>'platform',
                'p.plat_server' =>'plat_server',
            ]
        ],[
            'p.day',
            'p.player_id',
            'p.platform',
            'p.plat_server',
            'p.server_id',
            'p.is_pay',
            'p.contin_day',
            'p.contin_pay',
            'pt.rmb'
        ],$where + [
            'p.day' => $day
        ]);
        return array_column($list,null,'player_id');
    }





}